module variables_mod
  
  use constants_mod

  implicit none

  real(r8), dimension(:,:), allocatable :: u, v
  real(r8), dimension(:  ), allocatable :: lats, lons
  real(r8), dimension(:,:), allocatable :: dct_u
  real(r8), dimension(:,:), allocatable :: dct_v
  real(r8), dimension(:  ), allocatable :: power_u, power_v
  real(r8), dimension(:  ), allocatable :: wavelength, wavenumber
  integer :: nlats, nlons, Nwave
  integer :: nlats_in, nlons_in
  real(r8) :: dlon, dlat

contains

subroutine allocate_vars(nlons_in, nlats_in, Nwave, nlons, nlats)

  integer, intent(in) :: nlons_in, nlats_in, Nwave, nlons, nlats

  if (.not. allocated(lats       )) allocate(lats      (nlats_in            ))
  if (.not. allocated(lons       )) allocate(lons      (nlons_in            ))
  if (.not. allocated(u          )) allocate(u         (nlons_in, nlats_in  ))
  if (.not. allocated(v          )) allocate(v         (nlons_in, nlats_in  ))
  if (.not. allocated(dct_u      )) allocate(dct_u     (0:nlons-1, 0:nlats-1))
  if (.not. allocated(dct_v      )) allocate(dct_v     (0:nlons-1, 0:nlats-1))
  if (.not. allocated(power_u    )) allocate(power_u   (Nwave               ))
  if (.not. allocated(power_v    )) allocate(power_v   (Nwave               ))
  if (.not. allocated(wavelength )) allocate(wavelength(Nwave               ))
  if (.not. allocated(wavenumber )) allocate(wavenumber(Nwave               ))

end subroutine allocate_vars

subroutine deallocate_vars()

  if (allocated(u         )) deallocate(u         )
  if (allocated(v         )) deallocate(v         )
  if (allocated(lats      )) deallocate(lats      )
  if (allocated(lons      )) deallocate(lons      )
  if (allocated(dct_u     )) deallocate(DCT_u     )
  if (allocated(dct_v     )) deallocate(dct_v     )
  if (allocated(power_u   )) deallocate(power_u   )
  if (allocated(power_v   )) deallocate(power_v   )
  if (allocated(wavelength)) deallocate(wavelength)
  if (allocated(wavenumber)) deallocate(wavenumber)
  
end subroutine deallocate_vars

end module variables_mod